<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/modules/include/taglib.jsp"%>
<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<title>购物车</title>
		<%@ include file="/WEB-INF/views/modules/include/meta.jsp"%>
	</head>
	<body>
	<!--导航条-->
	<%@ include file="/WEB-INF/views/modules/include/top.jsp"%>
	
	<!-- 内容 -->
    <div class="container" style="margin-top: 40px;">
			<%@ include file="/WEB-INF/views/modules/include/left.jsp"%>
			
			<div class="col-md-6" style="left: 25%;">
				<%@ include file="/WEB-INF/views/modules/include/navicat.jsp"%>
				
                <div class="row">
                    <div class="col-md-12 text-center" id="cart_list_data">
						
					</div>
					
					<!-- 空购物车 -->
					<div class="col-md-12 text-center" style="display: none; height: 200px;" id="cart_null_data">
				        <br>
				        <a href="${ctxShop}/shoping/goodList.shtml?sid=${sid}&classifyId=${classifyId}&mode=${mode}">
				            <img class="body_bottom" alt="" src="${ctxStatic}/modules/bootstrap/img/gwc_0.png"  >
				            <span class="badge">您的购物车还没有商品</span>
				        </a>
				        <br><br><br>
				        <a href="${ctxWeb}">
				            <button class="btn btn-default" type="submit">
				                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				                	返回首页选购商品
				                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				            </button>
				        </a>
				    </div>
                </div>
			</div>
			
			<%@ include file="/WEB-INF/views/modules/include/right.jsp"%>
   </div>
   
   <!-- 底部 -->
	<nav class="container navbar navbar-default navbar-fixed-bottom pc_bottom_width" style="width: 50%;">
		<div class="container-fluid text-center">
               <div class="row">
               		<div class="col-md-3 pc_color">
                      	<p>总金额：<span class="pc_amount">￥<small id="sl">0.00</small></span></p>
                      	<span id="car_num" style="display: none;"></span>
                   </div>
                   <div class="col-md-6 pc_color" id="tranExpenses" style="display: none;">
                       <p style="font-size: 11px;">还差&nbsp;<span style="font-size: 11px; color: #000;">￥<font id="tranExpensesFee">0.00</font></span>&nbsp;免运费</p>
                   </div>
                   <div class="col-md-2 pc_right" style="padding-top: 8px;" align="c">
                   		<a href="javascript:toSettlement('${ctxShop}/shoping/orderInfo/toSettlement?sid=${sid}&classifyId=${classifyId}&shopModel=${shopModel}&mode=${mode}')"><button type="button" class="btn btn-danger">去结算</button></a>
                   </div>
               </div>
           </div>
	</nav>
<script type="text/javascript">
var lowFee = ${lowFee};
var fee = ${fee};
/**初始化购物车列表*/
var cookieService = new  MallCookieService();
var products = cookieService.getCarsObject();
initProductCartList();
			
/**
 * 初始化购物车商品列表
 */
function initProductCartList(){
	var productCarPriceSum = 0;
	var productCarOrderNum = 0;
	products = cookieService.getCarsObject();
	
	if(products != null && products.length > 0){
		$("#cart_list_data").show();
		$("#cart_null_data").hide();
		
		var tr = "";
		$.each(products,function(i,product){
			productCarPriceSum += (numberConvertor(product.price) * numberConvertor(product.getNumber()));
			productCarOrderNum += numberConvertor(product.getNumber());
			
			//$("#sl_product" +product.getId()).text(product.getNumber());
			//加载页面数据
			var url = "${ctxShop}/shoping/goodInfo/productDetail.shtml?id="+product.id;
			tr += "<div class=\"row\" style=\"padding-top: 15px; margin-left: 15px; margin-right: 15px; padding-bottom: 10px; border-bottom: 2px solid #D5E9BE;\" id=\"row"+product.id+"\">";
			tr += "<div class=\"col-md-12 col-sm-12 col-xs-12\">";
			tr += "<div id=\""+product.id+"\" class=\"media\">";
			tr += "<div class=\"media-left\" onclick=\"showProductDetail('"+url+"');\">";
			tr += "<a href=\"javascript:\">";
                     tr += "<img id=\"photo"+i+"\" class=\"displayImages media-object body_product_img pc_img\"";
			tr += "src=\"${ctxFront}/images/loading.png\" path=\"${ctxWeb}/shops/download?filePath="+product.logoUrl +"\"";
			tr += "data-holder-rendered=\"true\" />";
                     tr += " </a>" ;
			tr += "</div>" ;
			tr += "<div class=\"media-body text-left\">" ;
			tr += "<a href=\"javascript:;\" onclick=\"onclick=\"showProductDetail('"+url+"');\"\"><strong>"+product.name+"</strong></a>" ;
			tr += " </br>" ;
			tr += "<label>单价：<font color=\"red\">￥"+product.price+"</font></label>" ;
			tr += "<div class=\"input-group input-group-sm pc_text\">" ;
	        tr += "<img onclick=\"mall_diffGoodCart('"+product.id+"', '"+i+"');\" id=\"imgdiff"+product.id+"\" class=\"pc_left pc_cursor\" status=\"0\" alt=\"\" src=\"${ctxStatic}/modules/bootstrap/img/diff-2.png\" width=\"25\" height=\"25\"/>";
	       	tr += "<label class=\"pc_lable pc_left\"><strong id=\"sl_product"+product.id+"\">"+product.getNumber()+"</strong></label>" ;
	        tr += "<img onclick=\"mall_addGoodCart('"+product.id+"', '"+i+"');\" id=\"imgadd"+product.id+"\" class=\"pc_left pc_cursor\" status=\"1\" alt=\"\" src=\"${ctxStatic}/modules/bootstrap/img/add-2.png\" width=\"25\" height=\"25\"/>" ;
			tr += "</div>" ;
			tr += "</div>";
			tr += "<div class=\"media-body text-left\">" ;
			tr += "<div class=\"pc_right\">" ;
			tr += "<p><strong>距离失效</strong></p>" ;
			tr += "<p>" ;
			
			tr += "<input type=\"hidden\" id=\"productId\" name=\"productId\" value=\""+product.getId()+"\"/>" ;
			tr += "<input type=\"hidden\" id=\"number\" name=\"number\" value=\""+product.getNumber()+"\"/>" ;
			tr += "<input type=\"hidden\" id=\"price\" name=\"price\" value=\""+product.getPrice()+"\"/>" ;
			tr += "<input type=\"hidden\" id=\"updateDate\" name=\"updateDate\" value=\""+product.getUpdateDate()+"\"/>" ;
			tr += "<input type=\"hidden\" id=\"effectiveTime\" name=\"effectiveTime\" value=\"1\"/>";
			
			tr += "<span class=\"label label-default hours\" style=\"padding: .2em .4em .3em; background-color: #938B8A;\">00</span>:" ;
			tr += "<span class=\"label label-default minutes\" style=\"padding: .2em .4em .3em; background-color: #938B8A;\">00</span>:" ;
			tr += "<span class=\"label label-default seconds\" style=\"padding: .2em .4em .3em; background-color: #938B8A;\">00</span>" ;
			tr += "</p>" ;
			tr += "<p class=\"pc_right\" style=\"margin-top: 5px; margin-right: 3px;\">" ;
			tr += "<span class=\"label label-default pc_cursor\" onclick=\"delProduct('"+product.id+"')\" style=\"padding: .2em .4em .3em; background-color: #938B8A;\">&nbsp;X&nbsp;</span>" ;
			tr += "</p>" ;
			tr += "</div>";
			tr += "</div>";
			tr += "</div>";
			tr += "</div>";
			tr += "</div>" ;
		});
		$("#cart_list_data").empty();
		$("#cart_list_data").html(tr);
		//预加载图片
		startLoadImage();
	}else{
		$("#cart_list_data").hide();
		$("#cart_null_data").show();
	}
	initProductCar();
	chageTranExpenses(productCarPriceSum);
}

/**
 * 初始化购物车商品列表
 */
function initProductCar(){
	var productCarPriceSum = 0;
	var productCarOrderNum = 0;
	
	var cookieService = new  MallCookieService();
	var cars = cookieService.getCarsObject();
	
	if(cars != null && cars.length > 0){
		$.each(cars,function(i,cookieCarProduct){
			productCarPriceSum += (numberConvertor(cookieCarProduct.price) * numberConvertor(cookieCarProduct.getNumber()));
			productCarOrderNum += numberConvertor(cookieCarProduct.getNumber());
			
			var number=cookieCarProduct.getNumber();
			var productId=cookieCarProduct.getId();
			var purchaseNum=cookieCarProduct.getPurchaseNum();
			var startSaleNum=cookieCarProduct.getStartSaleNum()
			
			if(number==startSaleNum){
				$("#imgdiff"+productId).attr("src",ctxStatic+"/modules/bootstrap/img/diff-1.png");
			}

			if(number==purchaseNum && purchaseNum>0){
				$("#imgadd"+productId).attr("src",ctxStatic+"/modules/bootstrap/img/add-1.png");
			}
			
		});
	}
	showProductCar(productCarPriceSum,productCarOrderNum,null,null);
}

//判断是否有运费
function chageTranExpenses(productCarPriceSum){
	if(productCarPriceSum<lowFee){
		$("#tranExpensesFee").text((lowFee-productCarPriceSum).toFixed(2));
		$("#tranExpenses").show();
	}else{
		$("#tranExpenses").hide();
	}
}

function delProduct(id){
	//if(window.confirm("确定要删除吗？")){
		products = cookieService.getCarsObject();
		var product=cookieService.findByIdProduct(id);
		if(products != null && products.length > 0){
			$.each(products,function(i,product){
				if(product.id==id){
					cookieService.delProduct(product);
				}
			});
			//initProductCartList();
			$("#row"+id).hide();
			//减去金额
			var amount=$("#sl").text();
			var subAmount=numberConvertor(amount)-numberConvertor(product.price) * numberConvertor(product.number);
			$("#sl").text(subAmount.toFixed());
			chageTranExpenses(subAmount.toFixed());
			
			if(products.length<=1){
				initProductCartList();
			}
		}
	//}
}

function diffOrAdd(price,op){
	var tranExpensesFee = $("#tranExpensesFee").text();
	var nowPrice=0;
	if(op=="add"){
		nowPrice=tranExpensesFee-price;
	}else{
		nowPrice=tranExpensesFee+price;
	}
	
	if(nowPrice<20){
		$("#tranExpensesFee").text((20-nowPrice).toFixed(2));
		$("#tranExpenses").show();
	}else{
		$("#tranExpenses").hide();
	}
}

/**
 * 减少购买商品
 */
function mall_diffGoodCart(productId,index)
{
	var cookieService = new  MallCookieService();
	var productCarPriceSum = 0;//总价格
	var productCarOrderNum = 0;//订单总数
	var productCurrentNum = 0;//当前商品显示数量
	
	/**获取当前商品对象*/
	var product = new Product();
	for(var i in products){
		if(productId==products[i].id){
			product = products[i];
		}
	}
	
	/**验证cookie是否存在*/
	var cookieProduct = cookieService.findByIdProduct(productId);
	if(cookieProduct == null ){
		alert("抱歉，当前商品限起购数量 "+product.startSaleNum+" 件，不能再低了。");
		return;
	}else{
		productCurrentNum = cookieProduct.getNumber()-1;
		if(productCurrentNum < product.startSaleNum){
			alert("抱歉，当前商品限起购数量 "+product.startSaleNum+" 件，不能再低了。");
			return;
		}else{
			cookieService.delProduct(cookieProduct);
			if(productCurrentNum > 0 ){
				cookieProduct.setNumber(productCurrentNum);
				cookieProduct.setUpdateDate(getNowDate());
				cookieService.addProduct(cookieProduct);
				//更新计时器
				var nowDate = new Date();
				var updateDate = nowDate.getFullYear()+"-"+(parseInt(nowDate.getMonth())+1)+"-"+nowDate.getDate()+" "+nowDate.getHours()+":"+nowDate.getMinutes()+":"+nowDate.getSeconds();
				$("input[id='updateDate']:eq("+index+")").val(updateDate);
			}else{
				productCurrentNum = 0;
			}
		}
	}
	
	if(productCurrentNum <= product.startSaleNum){
		$("#imgdiff"+product.id).attr("src",ctxStatic+"/modules/bootstrap/img/diff-1.png");
	}
	$("#imgadd"+product.id).attr("src",ctxStatic+"/modules/bootstrap/img/add-2.png");
	
	/**获取购物车商品*/
	var cars = cookieService.getCarsObject();
	if(cars != null && cars.length > 0){
		$.each(cars,function(i,cookieCarProduct){
			productCarPriceSum += (cookieCarProduct.price * cookieCarProduct.getNumber());
			productCarOrderNum += numberConvertor(cookieCarProduct.getNumber());
		});
	}
	showProductCar(productCarPriceSum,productCarOrderNum,productCurrentNum,productId);
	chageTranExpenses(productCarPriceSum);
}

/**
 * 添加购买商品数量
 * @param {Object} productId
 * @return {TypeName} 
 */
function mall_addGoodCart(productId,index){
	var cookieService = new  MallCookieService();
	var productCarPriceSum = 0;//总价格
	var productCarOrderNum = 0;//订单总数
	var productCurrentNum = 0;//当前商品显示数量
	
	/**获取当前商品对象*/
	var product = new Product();
	for(var i in products){
		if(productId==products[i].id){
			product = products[i];
		}
	}
	
	/**验证cookie是否存在*/
	var cookieProduct = cookieService.findByIdProduct(productId);
	
	/**新购商品*/
	if(cookieProduct == null ){
		cookieProduct = product;
		productCurrentNum = product.getStartSaleNum();
		cookieProduct.setNumber(productCurrentNum);
		cookieProduct.setUpdateDate(getNowDate());
		cookieService.addProduct(cookieProduct);
		//更新计时器
		var nowDate = new Date();
		var updateDate = nowDate.getFullYear()+"-"+(parseInt(nowDate.getMonth())+1)+"-"+nowDate.getDate()+" "+nowDate.getHours()+":"+nowDate.getMinutes()+":"+nowDate.getSeconds();
		$("input[id='updateDate']:eq("+index+")").val(updateDate);
	
		/**已经存在购物车*/
	}else{
		productCurrentNum = numberConvertor(cookieProduct.getNumber()) + 1;
		
		/**是否设置限购*/
		if(product.getPurchaseNum() > 0 && productCurrentNum > product.getPurchaseNum()){
			alert("抱歉，当前商品限购数量 "+product.purchaseNum+" 件，不能再高了。");
			return;
		}
		
		/**保存数量*/
		cookieService.delProduct(cookieProduct);
		cookieProduct.setNumber(productCurrentNum);
		cookieProduct.setUpdateDate(getNowDate());
		cookieService.addProduct(cookieProduct);
		//更新计时器
		var nowDate = new Date();
		var updateDate = nowDate.getFullYear()+"-"+(parseInt(nowDate.getMonth())+1)+"-"+nowDate.getDate()+" "+nowDate.getHours()+":"+nowDate.getMinutes()+":"+nowDate.getSeconds();
		$("input[id='updateDate']:eq("+index+")").val(updateDate);
		
	}
	
	if(product.purchaseNum > 0 && productCurrentNum == product.purchaseNum){
		$("#imgadd"+product.id).attr("src",ctxStatic+"/modules/bootstrap/img/add-1.png");
	}
	$("#imgdiff"+product.id).attr("src",ctxStatic+"/modules/bootstrap/img/diff-2.png");
	
	/**获取购物车商品*/
	var cars = cookieService.getCarsObject();
	
	if(cars != null && cars.length > 0){
		$.each(cars,function(i,cookieCarProduct){
			productCarPriceSum += (numberConvertor(cookieCarProduct.price) * numberConvertor(cookieCarProduct.getNumber()));
			productCarOrderNum += numberConvertor(cookieCarProduct.getNumber());
		});
	}
	showProductCar(productCarPriceSum,productCarOrderNum,productCurrentNum,productId);
	chageTranExpenses(productCarPriceSum);
}
 
function toSettlement(url){
	location.href=url;
}

//定时器更新抢购活动时间
setInterval("countdownTimer()",1000);//1000为1秒钟
function countdownTimer(){
	$("input[name='updateDate']").each(function(i){
		var updateDate = $(this).val();
		var effectiveTime = parseInt($("input[id='effectiveTime']:eq("+i+")").val())*60*60*1000;
		var endDate = new Date(updateDate.replace(/\-/g, "\/"));
		var nowDate = new Date();
		var time=(endDate.getTime()-nowDate.getTime())+effectiveTime;//毫秒
		if(time<=0){
			//清空这条购物车
			var id=$("input[id='productId']:eq("+i+")").val();
			delProduct(id);
		}else
			reckonTime(i,time);
	});
}
function reckonTime(index,time){
	var hours=(time/(1000*60*60));
	hours = parseInt(hours);
	$(".hours:eq("+index+")").text(hours<10?0+""+hours:hours);
	time=time-hours*(1000*60*60);
	var minutes = time/(1000*60);
	minutes = parseInt(minutes);
	$(".minutes:eq("+index+")").text(minutes<10?0+""+minutes:minutes);
	time=time-minutes*(1000*60);
	var seconds = time/1000;
	seconds = parseInt(seconds);
	$(".seconds:eq("+index+")").text(seconds<10?0+""+seconds:seconds);
}


		function showProductDetail(url){
			var sid="${merchant.id}";
			var choiceClassifyId = "${choiceClassifyId}";
			//loading('正在跳转，请稍等...');
			setTimeout(function(){
				//hideLoader();
				document.location=url+"&sid="+sid+"&classifyId="+choiceClassifyId;				
			},1000);
		}
</script>
</body>
</html>
